package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysUserAddDTO;
import com.framework.system.entity.dto.SysUserQueryDTO;
import com.framework.system.entity.dto.SysUserResetPwdDTO;
import com.framework.system.entity.dto.SysUserUpdateDTO;
import com.framework.system.entity.vo.AccountInfoVO;
import com.framework.system.entity.vo.SysUserListVO;
import com.framework.system.entity.vo.SysUserVO;
import com.framework.system.entity.vo.UserInfoVO;
import com.framework.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Set;

/**
 * 用户管理
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysUser")
@AllArgsConstructor
public class SysUserController {

    private ISysUserService sysUserService;

    /**
     * 分页查询用户列表
     */
    @GetMapping("/selectSysUserWithPage")
    @SaCheckPermission("system:sysUser:selectSysUserWithPage")
    @AutoLog(module = "用户管理", title = "分页查询用户")
    public R<Page<SysUserListVO>> selectSysUserWithPage(Page reqPage, SysUserQueryDTO req) {
        return R.success(sysUserService.selectSysUserWithPage(reqPage, req));
    }

    /**
     * 新增用户
     */
    @PostMapping("/addSysUser")
    @SaCheckPermission("system:sysUser:addSysUser")
    @AutoLog(module = "用户管理", title = "新增用户")
    public R<Void> addSysUser(@Validated @RequestBody SysUserAddDTO req) {
        sysUserService.addSysUser(req);
        return R.success();
    }

    /**
     * 修改用户
     */
    @PostMapping("/updateSysUser")
    @SaCheckPermission("system:sysUser:updateSysUser")
    @AutoLog(module = "用户管理", title = "修改用户")
    public R<Void> updateSysUser(@Validated @RequestBody SysUserUpdateDTO req) {
        sysUserService.updateSysUser(req);
        return R.success();
    }

    /**
     * 删除用户
     */
    @PostMapping("/removeSysUser")
    @SaCheckPermission("system:sysUser:removeSysUser")
    @AutoLog(module = "用户管理", title = "删除用户")
    public R<Void> removeSysUser(@RequestParam String ids) {
        sysUserService.removeSysUser(ids);
        return R.success();
    }

    /**
     * 查看用户
     */
    @GetMapping("/getSysUser")
    @SaCheckPermission("system:sysUser:getSysUser")
    @AutoLog(module = "用户管理", title = "查看用户")
    public R<SysUserVO> getSysUser(@RequestParam String id) {
        return R.success(sysUserService.getSysUser(id));
    }

    /**
     * 导出用户
     *
     * @param req
     * @param response
     */
    @GetMapping("/exportSysUser")
    @SaCheckPermission("system:sysUser:exportSysUser")
    @AutoLog(module = "用户管理", title = "导出用户")
    @SneakyThrows
    public void exportSysUser(SysUserQueryDTO req, HttpServletResponse response) {
        String filename = "用户列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysUserListVO.class)
                .sheet("用户列表")
                .doWrite(sysUserService.selectSysUserWithList(req));
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPwd")
    @SaCheckPermission("system:sysUser:resetPwd")
    @AutoLog(module = "用户管理", title = "重置密码")
    public R<Void> resetPwd(@Validated @RequestBody SysUserResetPwdDTO req) {
        sysUserService.resetPwd(req);
        return R.success();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    @AutoLog(module = "用户管理", title = "获取用户信息")
    public R<UserInfoVO> getUserInfo() {
        return R.success(sysUserService.getUserInfo());
    }

    /**
     * 获取权限集合
     *
     * @return
     */
    @GetMapping("/selectPermCodeWithList")
    @AutoLog(module = "用户管理", title = "获取权限集合")
    public R<Set<String>> selectPermCodeWithList() {
        return R.success(sysUserService.selectPermCodeWithList());
    }

    /**
     * 获取账户信息
     *
     * @return
     */
    @GetMapping("/getAccountInfo")
    @AutoLog(module = "用户管理", title = "获取账户信息")
    public R<AccountInfoVO> getAccountInfo() {
        return R.success(sysUserService.getAccountInfo());
    }

    /**
     * 保存用户信息
     *
     * @param req
     * @return
     */
    @PostMapping("/saveAccountInfo")
    @AutoLog(module = "用户管理", title = "保存用户信息")
    public R<Void> saveAccountInfo(@Validated @RequestBody AccountInfoVO req) {
        sysUserService.saveAccountInfo(req);
        return R.success();
    }

}

